package org.jetnuke.dao.hsqldb;

import org.apache.log4j.Logger;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.List;

import net.jforum.JForumExecutionContext;
import net.jforum.util.preferences.SystemGlobals;

import org.jetnuke.dao.generic.GenericLinkLinkDAO;

public class HsqldbLinkLinkDAO extends GenericLinkLinkDAO {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger
			.getLogger(HsqldbLinkLinkDAO.class);

	public List search(String title, String description, int offset,
			int rowcount) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("search(String, String, int, int) - start"); //$NON-NLS-1$
		}

		PreparedStatement p = JForumExecutionContext.getConnection()
				.prepareStatement(SystemGlobals.getSql("LinkLinkModel.search"));
		p.setInt(1, offset);
		p.setInt(2, rowcount);

		p.setString(3, title);
		p.setString(4, description);

		ResultSet rs = p.executeQuery();
		List list = this.processSelectAll(rs);
		rs.close();
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("search(String, String, int, int) - end"); //$NON-NLS-1$
		}
		return list;
	}

	public List selectDateAsc(int cid, int offset, int rowcount)
			throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("selectDateAsc(int, int, int) - start"); //$NON-NLS-1$
		}

		PreparedStatement p = JForumExecutionContext.getConnection()
				.prepareStatement(
						SystemGlobals.getSql("LinkLinkModel.selectDateAsc"));
		p.setInt(3, cid);
		p.setInt(1, offset);
		p.setInt(2, rowcount);
		ResultSet rs = p.executeQuery();
		List list = this.processSelectAll(rs);
		rs.close();
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("selectDateAsc(int, int, int) - end"); //$NON-NLS-1$
		}
		return list;
	}

	public List selectDateDesc(int cid, int offset, int rowcount)
			throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("selectDateDesc(int, int, int) - start"); //$NON-NLS-1$
		}

		PreparedStatement p = JForumExecutionContext.getConnection()
				.prepareStatement(
						SystemGlobals.getSql("LinkLinkModel.selectDateDesc"));
		p.setInt(3, cid);
		p.setInt(1, offset);
		p.setInt(2, rowcount);
		ResultSet rs = p.executeQuery();
		List list = this.processSelectAll(rs);
		rs.close();
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("selectDateDesc(int, int, int) - end"); //$NON-NLS-1$
		}
		return list;
	}

	public List selectHitsAsc(int cid, int offset, int rowcount)
			throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("selectHitsAsc(int, int, int) - start"); //$NON-NLS-1$
		}

		PreparedStatement p = JForumExecutionContext.getConnection()
				.prepareStatement(
						SystemGlobals.getSql("LinkLinkModel.selectHitsAsc"));
		p.setInt(3, cid);
		p.setInt(1, offset);
		p.setInt(2, rowcount);
		ResultSet rs = p.executeQuery();
		List list = this.processSelectAll(rs);
		rs.close();
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("selectHitsAsc(int, int, int) - end"); //$NON-NLS-1$
		}
		return list;
	}

	public List selectHitsDesc(int cid, int offset, int rowcount)
			throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("selectHitsDesc(int, int, int) - start"); //$NON-NLS-1$
		}

		PreparedStatement p = JForumExecutionContext.getConnection()
				.prepareStatement(
						SystemGlobals.getSql("LinkLinkModel.selectHitsDesc"));
		p.setInt(3, cid);
		p.setInt(1, offset);
		p.setInt(2, rowcount);
		ResultSet rs = p.executeQuery();
		List list = this.processSelectAll(rs);
		rs.close();
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("selectHitsDesc(int, int, int) - end"); //$NON-NLS-1$
		}
		return list;
	}

	public List selectRatingAsc(int cid, int offset, int rowcount)
			throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("selectRatingAsc(int, int, int) - start"); //$NON-NLS-1$
		}

		PreparedStatement p = JForumExecutionContext.getConnection()
				.prepareStatement(
						SystemGlobals.getSql("LinkLinkModel.selectRatingAsc"));
		p.setInt(3, cid);
		p.setInt(1, offset);
		p.setInt(2, rowcount);
		ResultSet rs = p.executeQuery();
		List list = this.processSelectAll(rs);
		rs.close();
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("selectRatingAsc(int, int, int) - end"); //$NON-NLS-1$
		}
		return list;
	}

	public List selectRatingDesc(int cid, int offset, int rowcount)
			throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("selectRatingDesc(int, int, int) - start"); //$NON-NLS-1$
		}

		PreparedStatement p = JForumExecutionContext.getConnection()
				.prepareStatement(
						SystemGlobals.getSql("LinkLinkModel.selectRatingDesc"));
		p.setInt(3, cid);
		p.setInt(1, offset);
		p.setInt(2, rowcount);
		ResultSet rs = p.executeQuery();
		List list = this.processSelectAll(rs);
		rs.close();
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("selectRatingDesc(int, int, int) - end"); //$NON-NLS-1$
		}
		return list;
	}

	public List selectTitleAsc(int cid, int offset, int rowcount)
			throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("selectTitleAsc(int, int, int) - start"); //$NON-NLS-1$
		}

		PreparedStatement p = JForumExecutionContext.getConnection()
				.prepareStatement(
						SystemGlobals.getSql("LinkLinkModel.selectTitleAsc"));
		p.setInt(3, cid);
		p.setInt(1, offset);
		p.setInt(2, rowcount);
		ResultSet rs = p.executeQuery();
		List list = this.processSelectAll(rs);
		rs.close();
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("selectTitleAsc(int, int, int) - end"); //$NON-NLS-1$
		}
		return list;
	}

	public List selectTitleDesc(int cid, int offset, int rowcount)
			throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("selectTitleDesc(int, int, int) - start"); //$NON-NLS-1$
		}

		PreparedStatement p = JForumExecutionContext.getConnection()
				.prepareStatement(
						SystemGlobals.getSql("LinkLinkModel.selectTitleDesc"));
		p.setInt(3, cid);
		p.setInt(1, offset);
		p.setInt(2, rowcount);
		ResultSet rs = p.executeQuery();
		List list = this.processSelectAll(rs);
		rs.close();
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("selectTitleDesc(int, int, int) - end"); //$NON-NLS-1$
		}
		return list;
	}

}
